/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*                                                                    *
*        ***************************************************         *
*        *©2021-2031 LINUO Corporation All rights reserved *         *
*        ***************************************************         *
*                                                                    *
* FileName    : avs.c                                                *
*                                                                    *
* Author      : linus                                                *
*                                                                    *
* Email       : luoyaojun@sina.com                                   *
*                                                                    *
* Date        : 2024-3-27                                            *
*                                                                    *
* Description :                                                      *
*                                                                    *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "h_avs.h"
#include "h_register.h"
#include "avs.h"

#include "rk_debug.h"
#include "rk_mpi_avs.h"
#include "rk_mpi_cal.h"
#include "rk_mpi_sys.h"
#include "rk_mpi_mb.h"


RK_S32 rk3588_avs_init(struct ht_avs *pavs)
{
	AVS_GRP s32GrpId = 0;
	AVS_CHN s32ChnId = 0;
	AVS_MOD_PARAM_S stAvsModParam;
	AVS_GRP_ATTR_S stAvsGrpAttr;
//	AVS_OUTPUT_ATTR_S stAvsOutAttr;
	AVS_CHN_ATTR_S stAvsChnAttr[4];
	int ret = -1;

	memset(&stAvsModParam, 0, sizeof(stAvsModParam));
	memset(&stAvsGrpAttr, 0, sizeof(stAvsGrpAttr));
	memset(&stAvsChnAttr[0], 0, sizeof(stAvsChnAttr[0]));
	stAvsModParam.u32WorkingSetSize = 67 * 1024;
	stAvsModParam.enMBSource = MB_SOURCE_PRIVATE;
	
	stAvsGrpAttr.enMode = AVS_MODE_BLEND;
	
	stAvsGrpAttr.stInAttr.enParamSource = AVS_PARAM_SOURCE_CALIB;
	stAvsGrpAttr.stInAttr.stCalib.pCalibFilePath = "/oem/usr/share/avs_calib/calib_file.pto";
	stAvsGrpAttr.stInAttr.stCalib.pMeshAlphaPath = "/oem/usr/share/avs_calib/";
	
	stAvsGrpAttr.u32PipeNum = 6;
	stAvsGrpAttr.stGainAttr.enMode = AVS_GAIN_MODE_AUTO;
	stAvsGrpAttr.stOutAttr.enPrjMode = 0;
	stAvsGrpAttr.stOutAttr.stCenter.s32X = 3840;
//	stAvsGrpAttr.stOutAttr.stCenter.s32Y = 100;
	stAvsGrpAttr.stOutAttr.stCenter.s32Y = 500;
	stAvsGrpAttr.stOutAttr.stFOV.u32FOVX = 36000;
	stAvsGrpAttr.stOutAttr.stFOV.u32FOVY = 5000;
	stAvsGrpAttr.stOutAttr.stORIRotation.s32Roll = 0;
	stAvsGrpAttr.stOutAttr.stORIRotation.s32Pitch = 0;
	stAvsGrpAttr.stOutAttr.stORIRotation.s32Yaw = 0;
	stAvsGrpAttr.stOutAttr.stRotation.s32Roll = 0;
	stAvsGrpAttr.stOutAttr.stRotation.s32Pitch = 0;
	stAvsGrpAttr.stOutAttr.stRotation.s32Yaw = 0;
	stAvsGrpAttr.bSyncPipe = RK_TRUE;
	stAvsGrpAttr.stFrameRate.s32SrcFrameRate = -1;
	stAvsGrpAttr.stFrameRate.s32DstFrameRate = -1;

	stAvsChnAttr[0].enCompressMode = COMPRESS_MODE_NONE;
	stAvsChnAttr[0].stFrameRate.s32SrcFrameRate = -1;
	stAvsChnAttr[0].stFrameRate.s32DstFrameRate = -1;
	stAvsChnAttr[0].u32Depth = 1;
	stAvsChnAttr[0].u32Width = 7680;
	stAvsChnAttr[0].u32Height = 1080;
	stAvsChnAttr[0].enDynamicRange = DYNAMIC_RANGE_SDR8;
	// 8 buffers are required to ensure that the post-level H264 reaches 30 frames
	
	stAvsChnAttr[0].u32FrameBufCnt = 8;
	ret = RK_MPI_AVS_SetModParam(&stAvsModParam);
	printf("LINUS RK_MPI_AVS_SetModParam = %d\n", ret);

	ret = RK_MPI_AVS_CreateGrp(s32GrpId, &stAvsGrpAttr);
	printf("LINUS RK_MPI_AVS_CreateGrp = %d\n", ret);

	ret = RK_MPI_AVS_SetChnAttr(s32GrpId, s32ChnId, &stAvsChnAttr[0]);
	printf("LINUS RK_MPI_AVS_SetChnAttr = %d\n", ret);
	ret = RK_MPI_AVS_EnableChn(s32GrpId, s32ChnId);
	printf("LINUS RK_MPI_AVS_EnableChn = %d\n", ret);
	ret = RK_MPI_AVS_StartGrp(s32GrpId);
	printf("LINUS RK_MPI_AVS_StartGrp = %d\n", ret);


	return 0;
}


__init static RK_S32 rk3588_avs_register(void)
{
	RK_S32 ret = -1;
	struct rk3588_avs *plt_avs = (struct rk3588_avs *)calloc(1, sizeof(struct rk3588_avs));
	struct ht_avs *pavs = NULL;
	
	if (!plt_avs)
		return -1;

	pavs = &plt_avs->m_avs;

	pavs->m_id = HT_ID_AVS;
	pavs->avs_init = rk3588_avs_init;

	ht_avs_module_register(pavs);

	return ret;
}

